package com.sqfw.project.knowledge.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import com.sqfw.project.knowledge.domain.KlCollectSort;
import com.sqfw.project.knowledge.domain.KlContentCollect;
import com.sqfw.project.knowledge.service.IKlContentCollectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.sqfw.framework.aspectj.lang.annotation.Log;
import com.sqfw.framework.aspectj.lang.enums.BusinessType;
import com.sqfw.framework.web.controller.BaseController;
import com.sqfw.framework.web.domain.AjaxResult;
import com.sqfw.common.utils.poi.ExcelUtil;
import com.sqfw.framework.web.page.TableDataInfo;

/**
 * 知识收藏Controller
 * 
 * @author tangxt
 * @date 2023-06-15
 */
@Api(tags = "知识库收藏")
@RestController
@RequestMapping("/klContentCollect")
public class KlContentCollectController extends BaseController {
    @Autowired
    private IKlContentCollectService klContentCollectService;

    /**
     * 查询知识收藏列表
     * @param klContentCollect
     * @return
     */
    @PreAuthorize("@ss.hasPermi('klContentCollect')")
    @GetMapping("/list")
    public TableDataInfo list(KlContentCollect klContentCollect) {
        startPage();
        List<KlContentCollect> list = klContentCollectService.selectKlContentCollectList(klContentCollect);
        return getDataTable(list);
    }

    /**
     * 导出知识收藏列表
     */
    @PreAuthorize("@ss.hasPermi('system:collect:export')")
    @Log(title = "知识收藏", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, KlContentCollect klContentCollect) {
        List<KlContentCollect> list = klContentCollectService.selectKlContentCollectList(klContentCollect);
        ExcelUtil<KlContentCollect> util = new ExcelUtil<KlContentCollect>(KlContentCollect.class);
        util.exportExcel(response, list, "知识收藏数据");
    }


    /**
     * 新增知识收藏
     * @param klContentCollect
     * @return
     */
    @ApiOperation("新增知识收藏")
    @PreAuthorize("@ss.hasPermi('klContentCollect')")
    @Log(title = "添加知识收藏", businessType = BusinessType.INSERT)
    @PostMapping("add")
    public AjaxResult add(@RequestBody @Valid KlContentCollect klContentCollect) {
        return klContentCollectService.insertKlContentCollect(klContentCollect);
    }

    /**
     * 删除知识收藏---根据id删除
     * @param klContentCollect
     * @return
     */
    @ApiOperation("删除知识收藏")
    @PreAuthorize("@ss.hasPermi('klContentCollect')")
    @Log(title = "删除知识收藏", businessType = BusinessType.DELETE)
	@DeleteMapping("remove")
    public AjaxResult remove(@RequestBody KlContentCollect klContentCollect) {
        if (StringUtils.isBlank(klContentCollect.getId())) {
            return AjaxResult.error("id必传");
        }
        return toAjax(klContentCollectService.deleteKlContentCollectById(klContentCollect.getId()));
    }


    // -------------------- 收藏分类

    /**
     * 添加收藏分类，或者新增子分类
     * @param klCollectSort
     * @return
     * */
    @ApiOperation("添加收藏分类")
    @Log(title = "添加收藏分类", businessType = BusinessType.INSERT)
    @PreAuthorize("@ss.hasPermi('klContentCollect')")
    @PostMapping("addKlCollectSort")
    public AjaxResult addKlCollectSort(@RequestBody @Valid KlCollectSort klCollectSort) {
        return klContentCollectService.addKlCollectSort(klCollectSort);
    }

    /**
     * 修改分类名称
     * @param klCollectSort
     * @return
     * */
    @ApiOperation("修改分类名称")
    @Log(title = "修改收藏分类", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('klContentCollect')")
    @PostMapping("updateKlCollectSort")
    public AjaxResult updateKlCollectSort(@RequestBody KlCollectSort klCollectSort) {
        if (StringUtils.isBlank(klCollectSort.getAutoId()) ||
                StringUtils.isBlank(klCollectSort.getSortName())) {
            return AjaxResult.error("分类名称不能为空");
        }
        return klContentCollectService.updateKlCollectSort(klCollectSort);
    }

    /**
     * 查询收藏分类
     * @return
     * */
    @ApiOperation("查询收藏分类")
    @PreAuthorize("@ss.hasPermi('klContentCollect')")
    @GetMapping("getKlCollectSortList")
    public AjaxResult getKlCollectSortList() {
        List<KlCollectSort> sortList = klContentCollectService.getKlCollectSortList();
        if (sortList == null) {
            return AjaxResult.error("没有收藏分类");
        }
        return AjaxResult.success(sortList);
    }

    /**
     * 删除收藏分类
     * @param klCollectSort
     * @return
     * */
    @ApiOperation("删除收藏分类")
    @Log(title = "删除收藏分类", businessType = BusinessType.DELETE)
    @PreAuthorize("@ss.hasPermi('klContentCollect')")
    @PostMapping("deleteKlCollectSort")
    public AjaxResult deleteKlCollectSort(@RequestBody KlCollectSort klCollectSort) {
        if (StringUtils.isBlank(klCollectSort.getAutoId())) {
            return AjaxResult.error("分类id不能为空");
        }
        return klContentCollectService.deleteKlCollectSort(klCollectSort);
    }


}
